home *** CD-ROM | disk | FTP | other *** search
- /************************************************************/
- /* */
- /* CStarterDoc Code from Chapter Six of */
- /* */
- /* *** The Macintosh Programming Primer *** */
- /* */
- /* Copyright 1990, Dave Mark */
- /* */
- /* This program demonstrates specific Mac programming */
- /* techniques. */
- /* */
- /************************************************************/
-
- /****
- * CStarterDoc.c
- *
- * Document methods for a typical application.
- *
- * Copyright © 1990 Symantec Corporation. All rights reserved.
- *
- ****/
-
- #include <Global.h>
- #include <Commands.h>
- #include <CApplication.h>
- #include <CBartender.h>
- #include <CDataFile.h>
- #include <CDecorator.h>
- #include <CDesktop.h>
- #include <CError.h>
- #include <CPanorama.h>
- #include <CScrollPane.h>
- #include "CStarterDoc.h"
- #include "CStarterPane.h"
- #include "TBUtilities.h"
- #include "CWindow.h"
- #include <Packages.h>
-
-
- #define WINDStarter 500 /* Resource ID for WIND template */
-
- extern CApplication *gApplication; /* The application */
- extern CBartender *gBartender; /* The menu handling object */
- extern CDecorator *gDecorator; /* Window dressing object */
- extern CDesktop *gDesktop; /* The enclosure for all windows */
- extern CBureaucrat *gGopher; /* The current boss in the chain of command */
- extern OSType gSignature; /* The application's signature */
- extern CError *gError; /* The global error handler */
-
- /***
- * IStarterDoc
- *
- * This is your document's initialization method.
- * If your document has its own instance variables, initialize
- * them here.
- *
- * The least you need to do is invoke the default method.
- *
- ***/
-
- /* Altered by TCL Demo Weaver 1.0 (2/21/90) */
-
- void CStarterDoc::IStarterDoc(CApplication *aSupervisor, Boolean printable)
-
- {
- CDocument::IDocument(aSupervisor, printable);
- }
-
-
- /***
- * Dispose
- *
- * This is your document's destruction method.
- * If you allocated memory in your initialization method
- * or opened temporary files, this is the place to release them.
- *
- * Be sure to call the default method!
- *
- ***/
-
- void CStarterDoc::Dispose()
-
- {
- inherited::Dispose();
- }
-
-
- /***
- * DoCommand
- *
- * This is the heart of your document.
- * In this method, you handle all the commands your document
- * deals with.
- *
- * Be sure to call the default method to handle the standard
- * document commands: cmdClose, cmdSave, cmdSaveAs, cmdRevert,
- * cmdPageSetup, cmdPrint, and cmdUndo. To change the way these
- * commands are handled, override the appropriate methods instead
- * of handling them here.
- *
- ***/
-
- void CStarterDoc::DoCommand(long theCommand)
-
- {
- switch (theCommand) {
-
- /* your document commands here */
-
- default: inherited::DoCommand(theCommand);
- break;
- }
- }
-
-
- /* Altered by TCL Demo Weaver 1.0 (2/21/90) */
-
- /***
- * UpdateMenus
- *
- * In this method you can enable menu commands that apply when
- * your document is active.
- *
- * Be sure to call the inherited method to get the default behavior.
- * The inherited method enables these commands: cmdClose, cmdSaveAs,
- * cmdSave, cmdRevert, cmdPageSetup, cmdPrint, cmdUndo.
- *
- ***/
-
- void CStarterDoc::UpdateMenus()
-
- {
- inherited::UpdateMenus();
-
- /* Enable your menu commands here (enable each one with a call to
- gBartender->EnableCmd(command_number)).
- */
-
- }
-
-
- /***
- * NewFile
- *
- * When the user chooses New from the File menu, the CreateDocument()
- * method in your Application class will send a newly created document
- * this message. This method needs to create a new window, ready to
- * work on a new document.
- *
- * Since this method and the OpenFile() method share the code for creating
- * the window, you should use an auxiliary window-building method.
- *
- ***/
- void CStarterDoc::NewFile(void)
-
- {
- /* Altered by TCL Demo Weaver 1.0 (2/21/90) */
-
- Str255 wTitle; /* Window title string. */
- short wCount; /* Index number of new window. */
- Str255 wNumber; /* Index number as a string. */
-
- /**
- ** BuildWindow() is the method that
- ** does the work of creating a window.
- ** Its parameter should be the data that
- ** you want to display in the window.
- ** Since this is a new window, there's nothing
- ** to display.
- **
- **/
-
- BuildWindow(NULL);
-
- /**
- ** Append an index number to the
- ** default name of the window.
- **/
-
- itsWindow->GetTitle(wTitle);
- wCount = gDecorator->GetWCount();
- NumToString(wCount, wNumber);
- ConcatPStrings(wTitle, "\p ");
- ConcatPStrings(wTitle, wNumber);
- itsWindow->SetTitle(wTitle);
-
- /**
- ** Send the window a Select() message to make
- ** it the active window.
- **/
-
- itsWindow->Select();
- }
-
-
- /***
- * OpenFile
- *
- * When the user chooses Open… from the File menu, the OpenDocument()
- * method in your Application class will let the user choose a file
- * and then send a newly created document this message. The information
- * about the file is in the SFReply record.
- *
- * In this method, you need to open the file and display its contents
- * in a window. This method uses the auxiliary window-building method.
- *
- ***/
-
- void CStarterDoc::OpenFile(SFReply *macSFReply)
-
- {
- CDataFile *theFile;
- Handle theData;
- Str63 theName;
- OSErr theError;
-
- /**
- ** Create a file and send it a SFSpecify()
- ** message to set up the name, volume, and
- ** directory.
- **
- **/
-
- theFile = new(CDataFile);
- theFile->IDataFile();
- theFile->SFSpecify(macSFReply);
-
- /**
- ** Be sure to set the instance variable
- ** so other methods can use the file if they
- ** need to. This is especially important if
- ** you leave the file open in this method.
- ** If you close the file after reading it, you
- ** should be sure to set itsFile to NULL.
- **
- **/
-
- itsFile = theFile;
-
- /**
- ** Send the file an Open() message to
- ** open it. You can use the ReadSome() or
- ** ReadAll() methods to get the contents of the file.
- **
- **/
-
- theFile->Open(fsRdWrPerm);
-
- /**
- ** Check to see if we were able to open
- ** the file. Send the error handler
- ** a CheckOSError() message. If there was
- ** an error, CheckOSError returns false
- ** and reports the error in an alert.
- ** The default error message displays the
- ** error number.
- ** You can use Estr resources to customize
- ** the error message.
- **
- ** Note that we send ourselves a Dispose()
- ** message. Since we're not going to open,
- ** we should get rid of the object.
- **/
-
- if (!gError->CheckOSError(theError)) {
- Dispose();
- return;
- }
-
- /**
- ** Make sure that the memory request to read
- ** the data from the file doesn't use up any
- ** of our rainy day fund and that the GrowMemory()
- ** method (in the application) knows that it's OK
- ** if we couldn't get enough memory.
- **
- **/
-
- /* Altered by TCL Demo Weaver 1.0 (2/21/90) */
-
- /*gApplication->RequestMemory(FALSE, TRUE);*/
- /*theFile->ReadAll(&theData); */ /* ReadAll() creates the handle */
-
- /* Reset canFail to FALSE for default memory-error handling. */
-
- /*gApplication->RequestMemory(FALSE, FALSE);*/
-
-
- /**
- ** If there isn't enough memory to open,
- ** post the error (should be -108)
- ** and get rid of ourselves.
- **
- **/
-
- if (theData == NULL) {
- gError->CheckOSError(MemError());
- Dispose();
- return;
- }
-
-
- BuildWindow(theData);
-
- /**
- ** In your application, you'll probably store
- ** the data in some form as an instance variable
- ** in your document class. For this example, there's
- ** no need to save it, so we'll get rid of it.
- **
- **/
-
- DisposHandle(theData);
-
- /**
- ** In this implementation, we leave the file
- ** open. You might want to close it after
- ** you've read in all the data.
- **
- **/
-
- itsFile->GetName(theName);
- itsWindow->SetTitle(theName);
- itsWindow->Select(); /* Don't forget to make the window active */
- }
-
-
-
- /***
- * BuildWindow
- *
- * Replace the old BuildWindow with this one...
- *
- ***/
-
- void CStarterDoc::BuildWindow (Handle theData)
- {
- CScrollPane *theScrollPane;
- CStarterPane *thePanorama;
- LongRect panFrame; /* the frame of the panorama */
-
- itsWindow = new( CWindow );
- itsWindow->IWindow( WINDStarter, FALSE, gDesktop, this );
-
- theScrollPane = new( CScrollPane );
-
- theScrollPane->IScrollPane( itsWindow, this, 0, 0, 0, 0, /* width, height = 0 */
- sizELASTIC, sizELASTIC,
- TRUE, TRUE, TRUE );
- theScrollPane->FitToEnclFrame( TRUE, TRUE );
- theScrollPane->SetSteps( 10, 10 );
-
- thePanorama = new( CStarterPane );
- thePanorama->IStarterPane( theScrollPane, this, 0, 0, 0, 0,
- sizELASTIC, sizELASTIC );
- thePanorama->FitToEnclosure( TRUE, TRUE );
- theScrollPane->InstallPanorama( thePanorama );
-
- itsMainPane = thePanorama;
- itsGopher = thePanorama;
-
- /* Make the panorama be as large as the interior of a zoomed-out window.
- (When the window is zoomed out, the scrollbars will be inactive; any
- smaller window will have active scrollbars. Alternatively, you could
- call GetFrame and SetBounds AFTER calling PlaceNewWindow; this would
- make the panorama as big as the interior of the default window.)
- */
-
- itsWindow->Zoom(inZoomOut);
- thePanorama->GetFrame(&panFrame);
- thePanorama->SetBounds(&panFrame);
-
- gDecorator->PlaceNewWindow( itsWindow );
-
- }
-
-
- /***
- * DoSave
- *
- * This method handles what happens when the user chooses Save from the
- * File menu. This method should return TRUE if the file save was successful.
- * If there is no file associated with the document, you should send a
- * DoSaveFileAs() message.
- *
- ***/
-
- Boolean CStarterDoc::DoSave(void)
-
- {
- /**
- ** If you closed your file in your NewFile() method,
- ** you'll need a different way than this to determine
- ** if there's a file associated with your document.
- **
- **/
-
- if (itsFile == NULL)
- return(DoSaveFileAs());
- else {
-
- /**
- ** In your application, this is where you'd
- ** write out your file. if you left it open,
- ** send the WriteSome() or WriteAll() mesages
- ** to itsFile.
- **
- **/
-
- dirty = FALSE; /* Document is no longer dirty */
- gBartender->DisableCmd(cmdSave);
- return(TRUE); /* Save was successful */
- }
- }
-
-
- /***
- * DoSaveAs
- *
- * This method handles what happens when the user chooses Save As… from
- * File menu. The default DoCommand() method for documents sends a DoSaveFileAs()
- * message which displays a standard put file dialog and sends this message.
- * The SFReply record contains all the information about the file you're about
- * to create.
- *
- ***/
-
- Boolean CStarterDoc::DoSaveAs(SFReply *macSFReply)
-
- {
- /**
- ** If there's a file associated with this document
- ** already, close it. The Dispose() method for files
- ** sends a Close() message to the file before releasing
- ** its memory.
- **
- **/
-
- if (itsFile != NULL)
- itsFile->Dispose();
-
-
- /**
- ** Create a new file, and then save it normally.
- **
- **/
-
- itsFile = new(CDataFile);
- ((CDataFile *)itsFile)->IDataFile();
- itsFile->SFSpecify(macSFReply);
- itsFile->CreateNew(gSignature, 'TEXT');
- itsFile->Open(fsRdWrPerm);
-
- itsWindow->SetTitle(macSFReply->fName);
-
- return( DoSave() );
- }
-
-
- /***
- * DoRevert
- *
- * If your application supports the Revert command, this method
- * should close the current file (without writing anything out)
- * and read the last saved version of the file.
- *
- ***/
-
- void CStarterDoc::DoRevert(void)
-
- {
- }
-